//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.x.springboot.util;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

public class DateUtils {
    private static final Pattern datePattern = Pattern.compile("(\\d{1,4}[-|\\/|年|\\.]\\d{1,2}[-|\\/|月|\\.]\\d{1,2}([日|号])?(\\s)*(\\d{1,2}([点|时])?((:)?\\d{1,2}(分)?((:)?\\d{1,2}(秒)?)?)?)?(\\s)*(PM|AM)?)", 10);
    public static final String DEF_DATETIME_FMT = "yyyy-MM-dd HH:mm:ss";
    public static final String DEF_DATE_FMT = "yyyy-MM-dd";
    public static final String DEF_DATE_FMT2 = "yyyyMMdd";
    public static final String DEF_DATETIME_FMT2 = "yyyy-MM-dd-HHmmss";
    public static final String DEF_TIME_FMT = "HH:mm:ss";
    public static final String DEF_DATETIME_FMT3 = "yyyyMMddHHmmss";
    public static final String DEF_DATETIME_FMT4 = "yyyy-MM-dd HH:mm";
    public static final String RANGE_DEFAULTTIME = "00:00:00 23:59:59";
    public static final String START_DEFAULTTIME = "00:00:00";
    public static final String END_DEFAULTTIME = "23:59:59";

    public DateUtils() {
    }

    public static Date adjust(Date originalDate, int type, int value) {
        Calendar cal = Calendar.getInstance();
        if (originalDate != null) {
            cal.setTime(originalDate);
        }

        if (value != 0) {
            cal.add(type, value);
        }

        return cal.getTime();
    }

    public static Date setTime(Date date, int hour, int minute, int second, int millisecond) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(11, hour);
        calendar.set(12, minute);
        calendar.set(13, second);
        calendar.set(14, millisecond);
        return calendar.getTime();
    }

    public static boolean compareDateValue(Date d1, Date d2) {
        if (d1 == null && d2 == null) {
            return true;
        } else if (d1 != null && d2 == null) {
            return false;
        } else if (d1 == null && d2 != null) {
            return true;
        } else {
            int year1 = getYearValue(d1);
            int year2 = getYearValue(d2);
            int monthValue1 = getMonthValue(d1);
            int monthValue2 = getMonthValue(d2);
            int dayValue1 = getDayValue(d1);
            int dayValue2 = getDayValue(d2);
            return year1 == year2 && monthValue1 == monthValue2 && dayValue1 == dayValue2;
        }
    }

    public static int getYearValue(Date d) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        return calendar.get(1);
    }

    public static int getMonthValue(Date d) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        return calendar.get(2) + 1;
    }

    public static int getDayValue(Date d) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        return calendar.get(5);
    }

    public static String format(Date date) {
        return format(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String format(Date date, String format) {
        if (date == null) {
            return "";
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(date);
        }
    }

    public static int getDaysInMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(year, month - 1, 1, 0, 0, 0);
        return cal.getActualMaximum(5);
    }

    public static int getDifferDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        clearTimeField(cal1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        clearTimeField(cal2);
        long between_days = (cal2.getTimeInMillis() - cal1.getTimeInMillis()) / 86400000L;
        return (int)between_days;
    }

    public static int getDifferMinutes(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        long between_Minutes = (cal2.getTimeInMillis() - cal1.getTimeInMillis()) / 60000L;
        return (int)between_Minutes;
    }

    public static Date getFirstDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        cal.set(5, 1);
        clearTimeField(cal);
        return cal.getTime();
    }

    public static Date getFirstDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.set(7, 2);
        clearTimeField(cal);
        return cal.getTime();
    }

    public static Date getLastDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        cal.set(5, cal.getActualMaximum(5));
        clearTimeField(cal);
        return cal.getTime();
    }

    public static Date getLastDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.set(7, 7);
        cal.add(5, 1);
        clearTimeField(cal);
        return cal.getTime();
    }

/*    public static Date getNow() {
        Calendar cal = Calendar.getInstance();
        return cal.getTime();
    }*/

    public static Date getToday() {
        Calendar cal = Calendar.getInstance();
        clearTimeField(cal);
        return cal.getTime();
    }

    public static boolean isMatchDatePattern(String date) {
        return datePattern.matcher(date).matches();
    }

    private static void clearTimeField(Calendar cal) {
        cal.set(11, 0);
        cal.set(12, 0);
        cal.set(13, 0);
        cal.set(14, 0);
    }
    
    
    /**
	 * java.util.Date转换成java.time.LocalDate
	 * @param date
	 * @return
	 */
	public static LocalDate dateToLocalDate(Date date) {
        if(null == date) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }
	
	
	/**
	 * java.time.LocalDate转换成java.util.Date
	 * @param localDate
	 * @return
	 */
	public static Date localDateToDate(LocalDate localDate) {
        if(null == localDate) {
            return null;
        }
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }


    public static Date getNowDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        ParsePosition pos = new ParsePosition(8);
        Date currentTime_2 = formatter.parse(dateString, pos);
        return currentTime_2;
    }

    public static Date stringtoDate(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;

        try {
            date = sdf.parse(time);
        } catch (ParseException var4) {
        }

        return date;
    }

    public static String getStringDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getStringDateShort() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getTimeShort() {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        Date currentTime = new Date();
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static Date strToDateLong(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    public static String dateToStrLong(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    public static String dateToStr(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    public static Date getNow() {
        Date currentTime = new Date();
        return currentTime;
    }

    public static Date getLastDate(long day) {
        Date date = new Date();
        long date_3_hm = date.getTime() - 122400000L * day;
        Date date_3_hm_date = new Date(date_3_hm);
        return date_3_hm_date;
    }

    public static String getStringToday() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getHour() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        String hour = dateString.substring(11, 13);
        return hour;
    }

    public static String getTime() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        String min = dateString.substring(14, 16);
        return min;
    }

    public static String getUserDate(String sformat) {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(sformat);
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static void main(String[] args) {
        System.out.println(getNow());
    }
}
